from flask import Flask, request, jsonify

from .paddle_ocr import PaddleOCRProcessor

app = Flask(__name__)
ocr_processor = PaddleOCRProcessor()


@app.route('/ocr', methods=['POST'])
def process_ocr():
    if 'file' not in request.files:
        return jsonify({'error': 'No file uploaded'}), 400

    file = request.files['file']
    if file.filename == '':
        return jsonify({'error': 'Empty filename'}), 400

    try:
        # 保存临时文件
        # file_path = f"E:/{file.filename}"
        # file.save(file_path)

        # 执行OCR识别
        # results = ocr_processor.process_image(file_path)
        results = ocr_processor.process_image1(file)
        return jsonify({
            'status': 'success',
            'results': results
        })
    except Exception as e:
        return jsonify({'error': str(e)}), 500



@app.route('/ocrImg', methods=['POST'])
def ocr_img():
    if 'file' not in request.files:
        return jsonify({'error': 'No file uploaded'}), 400

    file = request.files['file']
    if file.filename == '':
        return jsonify({'error': 'Empty filename'}), 400

    try:
        results = ocr_processor.process_image1(file)
        return jsonify({
            'status': 'success',
            'results': results
        })
    except Exception as e:
        return jsonify({'error': str(e)}), 500


if __name__ == '__main__':
    app.run(host='0.0.0.0')